package com.gable.exhibition.controller;

import com.alibaba.fastjson.JSON;
import com.gable.common.util.CacheData;
import com.gable.common.util.ReturnResultUtil;
import com.gable.common.util.StringUtil;
import com.gable.exhibition.model.User;
import com.gable.exhibition.service.TrainPersonService;
import com.gable.exhibition.service.TrainService;
import com.gable.exhibition.util.MessageUtil;
import com.gable.exhibition.view.TrainDailyView;
import com.gable.exhibition.view.TrainFileView;
import com.gable.exhibition.view.TrainPersonView;
import com.gable.exhibition.view.TrainView;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
     * Created by mj on 2018/1/4.
     */
    @Controller
    @RequestMapping(value = "/train")
    public class TrainController {

        Logger logger = Logger.getLogger(this.getClass());

        @Autowired
        TrainService trainService;
        @Autowired
        TrainPersonService trainPersonService;

        @RequestMapping(value = "/page")
        public String page(HttpServletRequest request, HttpServletResponse response){
            return "train/train-page";
        }

        @RequestMapping(value="/queryTrainList",method = RequestMethod.POST, produces="application/json;charset=utf-8")
        @ResponseBody
        public ReturnResultUtil queryTrainList(HttpServletRequest request, HttpServletResponse response,TrainView trainView){
            try{
                ReturnResultUtil rru = trainService.queryTrainList(trainView);
                logger.info(JSON.toJSONString(rru));
                return rru;
            }catch (Exception e){
                e.printStackTrace();
            }
            return new ReturnResultUtil(500, MessageUtil.QUERYEXCEPTION);
        }

        @RequestMapping(value="/queryTrainInfo",method = RequestMethod.POST, produces="application/json;charset=utf-8")
        @ResponseBody
        public ReturnResultUtil queryTrainInfo(HttpServletRequest request, HttpServletResponse response){
            try{
                if (StringUtil.isEmpty(request.getParameter("trainId"))) {
                    return new ReturnResultUtil(500, MessageUtil.TRAINIDDOESTNOEXIST);
                }

                Long trainId = Long.parseLong(request.getParameter("trainId"));

                ReturnResultUtil rru = trainService.queryTrainInfo(trainId);
                logger.info(JSON.toJSONString(rru));
                return rru;
            }catch (Exception e){
                e.printStackTrace();
            }
            return new ReturnResultUtil(500, MessageUtil.QUERYEXCEPTION);
        }

        @RequestMapping(value="/addTrainList",method = RequestMethod.POST, produces="application/json;charset=utf-8")
        @ResponseBody
        public ReturnResultUtil addTrainList(HttpServletRequest request, HttpServletResponse response,TrainView trainView){
            try {
                DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                User user = CacheData.getSessionUser(null,1);
                if(trainView.getTrainId() == null){
                    trainView.setCreatePerson(user.getUserId());
                    trainView.setCreateDate(df.format(new Date()));
                }else{
                    trainView.setUpdatePerson(user.getUserId());
                    trainView.setUpdateDate(df.format(new Date()));
                }
                String dailyJson = trainView.getDailyJson();
                List<TrainDailyView> trainDailyViews = JSON.parseArray(dailyJson, TrainDailyView.class);
                trainView.setDailyList(trainDailyViews);

                String personJson = trainView.getPersonJson();
                List<TrainPersonView> trainPersonViews = JSON.parseArray(personJson, TrainPersonView.class);
                trainView.setPersonList(trainPersonViews);

                String fileJson = trainView.getFileJson();
                List<TrainFileView> trainFileViews = JSON.parseArray(fileJson, TrainFileView.class);
                trainView.setFileList(trainFileViews);
                return trainService.addTrain(trainView);
            }catch (Exception e){
                e.printStackTrace();
            }
            return new ReturnResultUtil(500,MessageUtil.SAVEEXCEPTION);
        }

    @RequestMapping(value="/queryTrainPerson",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryTrainPerson(HttpServletRequest request, HttpServletResponse response){
        try{
            if (StringUtil.isEmpty(request.getParameter("trainId"))) {
                return new ReturnResultUtil(500, MessageUtil.TRAINIDDOESTNOEXIST);
            }

            Long trainId = Long.parseLong(request.getParameter("trainId"));
            ReturnResultUtil rru = trainPersonService.queryTrainPerson(trainId);
            logger.info(JSON.toJSONString(rru));
            return rru;
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ReturnResultUtil(500, MessageUtil.QUERYEXCEPTION);
    }
}